const  fs = require('fs')
let path  = require('path');  // node内置模块。
const Koa = require("koa")
const cors = require("koa2-cors")
const helmet = require("koa-helmet")
const Router = require("koa-router")
const bodyParser = require("koa-bodyparser")
const consola = require('consola')
const { Nuxt, Builder } = require('nuxt')

const app = new Koa()

const router = new Router()

// Import and Set Nuxt.js options
const config = require('../nuxt.config.js')
config.dev = app.env !== 'production'

function useMiddleware(){
  app.use(helmet())
  app.use(bodyParser())
  // 设置全局返回头
  app.use(cors({
    origin(ctx) {
      return 'http://localhost:9000'; // cors
    },
    exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'],
    maxAge: 86400,
    credentials: true,  // 允许携带头部验证信息
    allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS'],
    allowHeaders: ['Content-Type', 'Authorization', 'Accept', 'X-Token', 'X-Device-Id', 'X-Uid'],
  }))
}

function useRouter(inpath){
  inpath = inpath || path.join(__dirname, '/routes')
  let urls = fs.readdirSync(inpath)
  urls.forEach((element) => {
    const elementPath = inpath + '/' + element
    const  stat = fs.lstatSync(elementPath);
    const isDir = stat.isDirectory();
    if (isDir){
      useRouter(elementPath)
    } else  {
      let module = require(elementPath)

      let  routeRrefix = inpath.split('/routes')[1] || ''
      router.use(routeRrefix + '/' + element.replace('.js', ''), module.routes())
    }
  })
  app.use(router.routes()).use(router.allowedMethods())
}

async function start () {
  // Instantiate nuxt.js
  const nuxt = new Nuxt(config)

  const {
    host = process.env.HOST || '127.0.0.1',
    port = process.env.PORT || 3000
  } = nuxt.options.server

  await nuxt.ready()
  // Build in development
  if (config.dev) {
    const builder = new Builder(nuxt)
    await builder.build()
  }

  useMiddleware()
  useRouter()
  app.use((ctx) => {
    ctx.status = 200
    ctx.respond = false // Bypass Koa's built-in response handling
    ctx.req.ctx = ctx // This mi
    nuxt.render(ctx.req, ctx.res)
  })

  app.listen(port, host)
  consola.ready({
    message: `Server listening on http://${host}:${port}`,
    badge: true
  })
}

start()
